package com.ykdl.member.kid.messagebox;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.adapters.FansAdapter;
import com.ykdl.member.kid.adapters.FansAdapter.OnAttentionListener;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class FansActivity extends BaseScreen implements OnClickListener, OnAttentionListener, OnItemClickListener{
	
	public static final String TAG = "FansActivity";
	
	private RefreshListView fans_list;
	private FansAdapter mAdapter;
	private TextView main_title, fans_num;
	private LinearLayout more_fans_ll;
	private View foot = null;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 50;
	private boolean isnewfans = false;
	
	private int actor_id;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fansactivity_xml);
		isnewfans = this.getIntent().getBooleanExtra("isnewfans", false);
		actor_id = getIntent().getIntExtra("actor_id", 0);
		init();
		getFasnList(actor_id, start_id, cursor, count, true);
		
	}
	
	@Override
	protected void onResume() {
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}

	public void init() {
//		foot = LayoutInflater.from(mContext).inflate(R.layout.fans_item_foot, null);
//				more_fans_ll = (LinearLayout) foot.findViewById(R.id.more_fans_ll);
//				foot.findViewById(R.id.more_fans).setOnClickListener(this);
		findViewById(R.id.btnBack).setOnClickListener(this);
		main_title = (TextView) findViewById(R.id.main_title);
		fans_num = (TextView) findViewById(R.id.fans_num);
		mAdapter = new FansAdapter(this);
		fans_list = (RefreshListView) findViewById(R.id.fans_list);
//		fans_list.addFooterView(foot);
		fans_list.setMoreButtoIsGon(false, "显示全部粉丝");
		fans_list.setOnItemClickListener(this);
		fans_list.setAdapter(mAdapter);
		mAdapter.setOnAttentionListener(this);
		fans_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isnewfans= false;
				getFasnList(actor_id, start_id, cursor, count, false);
			}});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.more_fans:
			main_title.setText("粉丝");
			fans_num.setVisibility(View.GONE);
			more_fans_ll.setVisibility(View.GONE);
			isnewfans= true;
			getFasnList(actor_id, start_id, cursor, count, false);
			break;
		case R.id.iask_root_loadmore:
			//Network.getInstance().requestBodyRecordList(cursor, count, type, mBodyListTag);
			isnewfans= true;
			getFasnList(actor_id, start_id, cursor, count, false);
			break;
		default:
			break;
		}
	}
	
	public void deltNewFansList(){
		String pageurl=KidConfig.DELT_NEWFANS_LIST;
		HttpParameters bodyParams = new HttpParameters();
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(pageurl, bodyParams);
		TaskManager.startHttpRequest(request, null, null);
	}
	public void getFasnList(int actor_id, int start_id, int cursor, int count, boolean show) {
		if (show) showProgress("正在发送请稍等...");
		String pageurl;
		if (isnewfans) {
			pageurl = KidConfig.GET_NEWFANS_LIST;
		}else {
			if (actor_id != 0) {
				pageurl = KidConfig.BASE_SERVER+"/api/v1/friendship/"+actor_id+"/followers";
			} else {
				pageurl = KidConfig.GET_FANS_LIST;
			}
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(pageurl, map, null);
		TaskManager.startHttpRequest(request, new GetFasnListTag(), Memberlistbean.class);
	}

	class GetFasnListTag implements ITag<Memberlistbean> {

		@Override
		public void refreshUI(Memberlistbean t, DataState ds) {
			finishProgress();
			if (t != null) {
				List<UserActorBean> beans = t.getList();
				if (beans!=null) {
					hidErrorTip( );
					mAdapter.addUser(beans);
					mAdapter.notifyDataSetChanged();
					if (t.getNext_cursor() == t.getTotal_number()) {
						fans_list.setMoreButtoIsGon(true);
					} else {
						cursor = t.getNext_cursor();
					}
					fans_num.setVisibility(View.VISIBLE);
					fans_num.setText(t.getTotal_number()+"人");
					
				}else {
					fans_num.setVisibility(View.GONE);
					showErrorTip("没有数据...", "");
				} 
				
			} else {
				Toast.makeText(FansActivity.this, "加载数据失败", 1).show();
			}
			if (isnewfans) {
				deltNewFansList();
			}
			fans_list.onRefreshComplete();
			fans_list.setFecthMoreOk();
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			finishProgress();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				Toast.makeText(FansActivity.this, "加载数据失败", 1).show();
				return true;
			} else {
				return false;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}

	}
	
	@Override
	public void onClickAttention(UserActorBean user) {
		showProgress("发送关注中...");
		int actor_id = user.getFollower_actor().getActor_id();
		Network.getInstance().addAttention(actor_id, new AttentionTag(user));
	}
	
	private class AttentionTag implements ITag<StatuResult> {
		private UserActorBean user;
		public AttentionTag(UserActorBean user){
			this.user = user;
		}
		
		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			finishProgress();
			if (t != null && t.getResult()==0 && TextUtils.isEmpty(t.getDesc())) {
				Toast.makeText(FansActivity.this, "关注成功", 1).show();
				user.setIs_friend(true);
				mAdapter.notifyDataSetChanged();
			} else {
				Toast.makeText(FansActivity.this, "关注失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			finishProgress();
			if (!TextUtils.isEmpty(parseError.getErrMsg())) {
				return false;
			} else {
				Toast.makeText(FansActivity.this, "关注失败", 1).show();
				return true;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		UserActorBean user = (UserActorBean) mAdapter.getItem(position - 1);
		Intent intent = new Intent(this, PersonCenterActivity.class);
		intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, user.getFollower_actor().getActor_id());
		startActivity(intent);
	}
}
